package com.ogawa.chair7808.ui.presenter;


import android.util.Log;

import com.easepal.socketiolib.model.SocketModel;
import com.ogawa.base.Constant.CommmandNum;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.observer.SocketDataObserver;
import com.ogawa.chair7808.ui.iview.IProMasDetailView7506;

import io.reactivex.disposables.Disposable;


/**
 * 专业按摩内页
 * Created by FHB on 2018/3/28
 */

public class ProMasDetailPresenter7506 implements SocketDataObserver.SocketReceive {
    IProMasDetailView7506 mView;

    private Disposable mDisposable;
    public ProMasDetailPresenter7506(IProMasDetailView7506 view) {
        mView = view;
        SocketDataObserver.getInst().registerObserver(this);
    }

    public void getTime(){
        DataManager.getDataManager().sendCommond(CommmandNum.mStatusTIME);
//        CommondManager.getInst().getState(CommmandNum.mStatusTIME, new GetStateCallback() {
//            @Override
//            public void onGetStateSuccess(BaseResponse<BeanStateInfo> program) {
//                if(program.getData().getCmdRsps()!=null){
//                    DataManager.getInst().putKeyValue(program.getData().getCmdRsps().get(0).getKey(),(program.getData().getCmdRsps().get(0).getValue()));
//
//                    String value = (program.getData().getCmdRsps().get(0).getValue());
//                    String min =value.substring(0,value.indexOf(":"));
//                    String sec =value.substring(value.indexOf(":")+1);
//                    if(!(min.equals("0")||min.equals("00"))&&!(sec.equals("0")||sec.equals("00"))){
//                        mView.getTime(min,sec);
//                    }
//
//                }
//                else {
//
//                }
//            }
//
//            @Override
//            public void onStateFailure() {
//
//            }
//        });
    }


    @Override
    public synchronized void onRceiveSocket(SocketModel model) {
        Log.e("closeActivity11",model.getKey()+"");

        String value = model.getValue();
        if(model.getKey().equals(CommmandNum.mStatusAuto)){
            getName(value);
            if(value.equals("0")){
                closeActivity();
            }
            else {
                getTime();
            }
        }
        //充气强度
        else if(model.getKey().equals(CommmandNum.mStatusAirStrong)){
            mView.getAir(Integer.valueOf(model.getValue()));
        }
        else if(model.getKey().equals(CommmandNum.mStatusRunning)){

            mView.isPause(value.equals("3"));
        }
        //3d
        else if(model.getKey().equals(CommmandNum.mStatus3D)){
            mView.get3D(Integer.valueOf(model.getValue()));
        }
        //力度
        else if(model.getKey().equals(CommmandNum.mStatusStrength)){
            mView.getStrength(Integer.valueOf(model.getValue()));
        }
        //速度
        else if(model.getKey().equals(CommmandNum.mStatusSpeed)){
            mView.getSpeed(Integer.valueOf(model.getValue()));
        }//肩部检测中
        else if(model.getKey().equals(CommmandNum.mStatusShoulderScanning)){
            if(value.equals("1")){
                mView.startToScan();
            }
        }
        else if(model.getKey().equals(CommmandNum.mStatusShoulderAdjust)){
            if(value.equals("1")){
                mView.startAdjust();
            }
        }


    }
    private void getName(String value){
        if(!value.equals("0")){
            String name ="";
            if(value.equals("1")){
                name = "全身舒展";
            }
            else if(value.equals("2")){
                name = "老年模式";
            }
            else if(value.equals("3")){
                name = "泰式拉伸";
            }
            else if(value.equals("4")){
                name = "速按放松";
            }
            else if(value.equals("5")){
                name = "清晨唤醒";
            }
            else if(value.equals("6")){
                name = "舒眠模式";
            } else if(value.equals("7")){
                name = "运动恢复";
            }
            else if(value.equals("8")){
                name = "深度放松";
            }
            else if(value.equals("9")){
                name = "上班族";
            }
            else if(value.equals("10")){
                name = "低头族";
            }
            else if(value.equals("11")){
                name = "开车族";
            }
            else if(value.equals("12")){
                name = "高跟族";
            } else if(value.equals("13")){
                name = "午间小休";
            }
            else if(value.equals("14")){
                name = "女王美体";
            }
            else if(value.equals("15")){
                name = "男士健体";
            }
            else if(value.equals("16")){
                name = "久坐释压";
            }
            else if(value.equals("17")){
                name = "酸痛检测";
            }
            else if(value.equals("18")){
                name = "肩颈呵护";
            }
            else if(value.equals("19")){
                name = "脊柱放松";
            }
            else if(value.equals("20")){
                name = "腰臀养护";
            }
            else if(value.equals("21")){
                name = "纤腿健足";
            }

            mView.getName(name);
        }
    }


    private void closeActivity(){
//        Observable.timer(200, TimeUnit.MILLISECONDS)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<Long>() {
//                    @Override
//                    public void onSubscribe(@NonNull Disposable d) {
//
//                        mDisposable =d;
//                    }
//
//                    @Override
//                    public void onNext(@NonNull Long aLong) {
//                        String hand = DataManager.getInst().getValue(CommmandNum.mStatusHand);
//                        String knead = DataManager.getInst().getValue(CommmandNum.mStatusKnead);
//                        Log.e("closeActivity",hand+"=="+knead);
//                        if(!hand.equals("0")||!knead.equals("0")){
//                            mView.startAdvance();
//                        }
//                        else {
//                            mView.closeActivity();
//
//                        }
//
//
//                        mDisposable.dispose();
//                    }
//
//                    @Override
//                    public void onError(@NonNull Throwable e) {
//                    }
//
//                    @Override
//                    public void onComplete() {
//                    }
//                });

    }

    public void onDestory(){
        SocketDataObserver.getInst().removeObserver(this);
    }
}
